package com.xxy.shiro.config;

import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;


@Configuration
public class ShiroConfig {


    /**
     * 创建 Realm
     * @return
     */
    @Bean
    public CustomizeRealm realm(){
        return new CustomizeRealm();
    }


    /**
     * 创建 DefaultWebSecurityManager
     * @return
     */
    @Bean
    public DefaultWebSecurityManager securityManager(){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(realm());
        return securityManager;
    }


    /**
     * 创建 ShiroFilterFactoryBean
     * @return
     */
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(){
        // 1, 创建 ShiroFilterFactoryBean 对象 用于创建 shiroFilter 过滤器
        ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
        // 2, 设置安全管理器 securityManager
        shiroFilter.setSecurityManager(securityManager());
        Map<String, String> map = new LinkedHashMap<>();
        map.put("/static/**","anon");
        map.put("/index","anon");
        map.put("/login","anon");
        map.put("/home","authc");
        // 3, 设置各种 URL
        shiroFilter.setLoginUrl("/login"); // 登录 URL
        shiroFilter.setSuccessUrl("/home"); // 登录成功 URL
        // 4, 设置 URL 的权限配置
        shiroFilter.setFilterChainDefinitionMap(map);
        return shiroFilter;
    }

}
